using System.Collections.Generic;
using System.Windows.Forms;
using RollManager.Library;
using RollManager.Forms.MainPanels;

namespace RollManager.Forms.DetailsPanels
{
    public partial class ServerGroupsDetailsPanel : UserControl, DetailsPanel
    {
        private readonly ServerGroup m_serverGroup;
        private readonly AdminActionsPanel m_panel;
        private int m_curIndex;
        private List<DevLevel> m_devLevels = Database.GetDevLevels();

        public ServerGroupsDetailsPanel( ServerGroup serverGroup, AdminActionsPanel panel )
        {
            InitializeComponent();

            m_serverGroup = serverGroup;
            m_panel = panel;

            // populate server group list.
            serversList.DisplayMember = ItemBase.NAME_PROPERTY;
            foreach( Server server in Database.GetServers( m_serverGroup.Id ) )
            {
                serversList.Items.Add( server );
            }

            // get all servers not in server group.
            List<Server> availableServers = Database.GetServers();
            foreach( Server server in serversList.Items )
            {
                availableServers.Remove( server );
            }

            // populate available servers list.
            availableServersList.DisplayMember = ItemBase.NAME_PROPERTY;
            foreach( Server server in availableServers )
            {
                availableServersList.Items.Add( server );
            }

            // populate sub action list.
            foreach(SubAction subAction in Database.GetSubActionsByServerGroup( m_serverGroup.Id ) )
            {
                Action action = Database.GetAction( subAction.ActionId );
                DevLevel level = getDevLevel( action.DevLevelId );

                subActionList.Items.Add(
                    string.Format( @"Dev Levels\{0}\{1}\{2}", level.Name, action.Name, subAction.Name ) );
            }

            nameBox.Text = m_serverGroup.Name;
        }

        private DevLevel getDevLevel( int devLevelId )
        {
            foreach( DevLevel level in m_devLevels )
            {
                if( level.Id == devLevelId )
                {
                    return level;
                }
            }
            return null;
        }

        public void Save()
        {
            if( Database.GetServerGroup( m_serverGroup.Id ) != null )
            {
                Database.ClearServerGroup( m_serverGroup.Id );

                for( int i = 0 ; i < serversList.Items.Count ; i++ )
                {
                    Database.AddServerToGroup( m_serverGroup.Id, ( (Server) serversList.Items[ i ] ).Id, i );
                }

                m_serverGroup.Name = nameBox.Text;
                Database.SaveServerGroup( m_serverGroup );
            }
        }

        private void serversList_MouseDown( object sender, MouseEventArgs e )
        {
            if( e.Button == MouseButtons.Left )
            {
                int index = serversList.IndexFromPoint( e.X, e.Y );
                if( index != -1)
                {
                    m_curIndex = index;
                }
            }
        }

        private void serversList_MouseMove( object sender, MouseEventArgs e )
        {
            if( e.Button == MouseButtons.Left )
            {
                int index = serversList.IndexFromPoint( e.X, e.Y );
                if( index != -1 && index != m_curIndex )
                {
                    object temp = serversList.Items[ index ];
                    serversList.Items[ index ] = serversList.Items[ m_curIndex ];
                    serversList.Items[ m_curIndex ] = temp;
                    m_curIndex = index;
                }
            }
        }

        private void availableServersList_DoubleClick( object sender, System.EventArgs e )
        {
            object server = availableServersList.SelectedItem;
            serversList.Items.Add( server );
            availableServersList.Items.Remove( server );
        }

        private void serversList_DoubleClick( object sender, System.EventArgs e )
        {
            object server = serversList.SelectedItem;
            availableServersList.Items.Add( server );
            serversList.Items.Remove( server );
        }

        private void subActionList_DoubleClick( object sender, System.EventArgs e )
        {
            m_panel.SelectNode( (string) subActionList.SelectedItem );
        }
    }
}